package com.szy.douyou_article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szy.douyou_article.config.ErrorMsgEnum;
import com.szy.douyou_article.config.ResultDTO;
import com.szy.douyou_article.dao.Article;
import com.szy.douyou_article.dao.Classification;
import com.szy.douyou_article.dao.mapper.ClassificationMapper;
import com.szy.douyou_article.dao.req.ClassificationReq;
import com.szy.douyou_article.service.ClassificationService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;


import java.util.List;

/**
 * @author sunziyan
 * @date 2022/02/08
 */
@Service
public class ClassificationServiceImpl implements ClassificationService {

    @Resource
    private ClassificationMapper classificationMapper;

    @Override
    public ResultDTO<Page<Classification>> queryAllClassification(ClassificationReq classificationReq) {
        Page<Classification> classificationPage = new Page<>(classificationReq.getPageNo(), classificationReq.getPageSize());


        classificationPage = (Page<Classification>) classificationMapper.selectPage(classificationPage
                , new QueryWrapper<Classification>()
                        .like("classification_name", classificationReq.getClassificationName()));
        return ResultDTO.success(classificationPage);
    }

    @Override
    public ResultDTO<Classification> queryOneClassification(Long id) {
        Classification classification = classificationMapper.selectById(id);

        if (classification == null) {
            return ResultDTO.fail(ErrorMsgEnum.RESULT_NOT_FOUND.desc());
        }

        return ResultDTO.success(classification);
    }

    @Override
    public ResultDTO<Boolean> addOneClassification(Classification classification) {


        Integer result = classificationMapper.insert(classification);

        if (result == 0) {
            return ResultDTO.fail(ErrorMsgEnum.INSERT_FAIL.desc());
        }
        return ResultDTO.success(true);
    }

    @Override
    public ResultDTO<Boolean> updateOneClassification(Classification classification) {

        if (classification == null) {
            return ResultDTO.fail(ErrorMsgEnum.PARAM_IS_NULL.desc());
        }

        Integer result = classificationMapper.updateById(classification);

        if (result == 0) {
            return ResultDTO.fail(ErrorMsgEnum.UPDATE_FAIL.desc());
        }
        return ResultDTO.success(true);
    }

    @Override
    public ResultDTO<Integer> deleteOneClassification(Long id) {

        if (id == null) {
            return ResultDTO.fail(ErrorMsgEnum.PARAM_IS_NULL.desc());
        }

        Integer result = classificationMapper.deleteById(id);
        classificationMapper.updateArticleClassificationWhenDelete(id);


        if (result == 0) {
            return ResultDTO.fail(ErrorMsgEnum.DELETE_FAIL.desc());
        }
        return ResultDTO.success(result);

    }


}
